<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="活动名称" prop="activityName">
        <el-input
          v-model="queryParams.activityName"
          placeholder="请输入活动名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="活动日期" prop="activityDate">
        <el-date-picker clearable
          v-model="queryParams.activityDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择活动日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:activity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:activity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:activity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:activity:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="活动ID" align="center" prop="activityId" />
      <el-table-column label="活动名称" align="center" prop="activityName" />
      <el-table-column label="活动类型" align="center" prop="activityType" />
      <el-table-column label="合作类型" align="center" prop="cooperationType" />
      <el-table-column label="活动日期" align="center" prop="activityDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.activityDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="活动描述" align="center" prop="activityDesc" />
      <el-table-column label="合同文件路径" align="center" prop="contractFile" />
      <el-table-column label="照片文件路径" align="center" prop="photoFile" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:activity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:activity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改专业活动记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="活动名称" prop="activityName">
          <el-input v-model="form.activityName" placeholder="请输入活动名称" />
        </el-form-item>
        
        <!-- 新增活动类型下拉选择 -->
        <el-form-item label="活动类型" prop="activityType">
          <el-select v-model="form.activityType" placeholder="请选择活动类型">
            <el-option label="研讨会" value="1" />
            <el-option label="培训" value="2" />
            <el-option label="竞赛" value="3" />
          </el-select>
        </el-form-item>
        
        <!-- 新增合作类型多选框 -->
        <el-form-item label="合作类型" prop="cooperationType">
          <el-checkbox-group v-model="form.cooperationType">
            <el-checkbox label="校企合作" value="校企合作" />
            <el-checkbox label="校内活动" value="校内活动" />
            <el-checkbox label="企业合作" value="企业合作" />
          </el-checkbox-group>
        </el-form-item>
        
        <el-form-item label="活动日期" prop="activityDate">
          <el-date-picker clearable
            v-model="form.activityDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择活动日期">
          </el-date-picker>
        </el-form-item>
        
        <el-form-item label="活动描述" prop="activityDesc">
          <el-input v-model="form.activityDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        
        <el-form-item label="合同文件路径" prop="contractFile">
          <file-upload v-model="form.contractFile"/>
        </el-form-item>
        
        <el-form-item label="照片文件路径" prop="photoFile">
          <file-upload v-model="form.photoFile"/>
        </el-form-item>
        
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity } from "@/api/system/activity";

export default {
  name: "Activity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 专业活动记录表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        activityName: null,
        activityType: null,
        cooperationType: null,
        activityDate: null,
        activityDesc: null,
        contractFile: null,
        photoFile: null,
        status: null,
      },
      // 表单参数
      form: {
        cooperationType: [] // 初始化合作类型为数组
      },
      // 表单校验
      rules: {
        activityName: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        activityType: [
          { required: true, message: '请选择活动类型', trigger: 'change' }
        ],
        cooperationType: [
          { required: true, message: '请选择合作类型', trigger: 'change' }
        ],
        activityDate: [
          { required: true, message: '请选择活动日期', trigger: 'change' }
        ],
        contractFile: [
          { required: true, message: '请上传合同文件', trigger: 'change' }
        ],
        photoFile: [
          { required: true, message: '请上传照片文件', trigger: 'change' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询专业活动记录列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        activityId: null,
        activityName: null,
        activityType: null,
        cooperationType: [], // 确保合作类型初始化为空数组
        activityDate: null,
        activityDesc: null,
        contractFile: null,
        photoFile: null,
        status: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.activityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加专业活动记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const activityId = row.activityId || this.ids
      getActivity(activityId).then(response => {
        this.form = response.data;
        
        // 处理合作类型回显：将字符串转换为数组
        if (this.form.cooperationType && typeof this.form.cooperationType === 'string') {
          // 假设数据库中存储的是逗号分隔的字符串
          this.form.cooperationType = this.form.cooperationType.split(',');
        }
        
        this.open = true;
        this.title = "修改专业活动记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 将合作类型数组转换为逗号分隔的字符串（如果后端需要）
          if (this.form.cooperationType && Array.isArray(this.form.cooperationType)) {
            this.form.cooperationType = this.form.cooperationType.join(',');
          }
          
          if (this.form.activityId != null) {
            updateActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addActivity(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const activityIds = row.activityId || this.ids;
      this.$modal.confirm('是否确认删除专业活动记录编号为"' + activityIds + '"的数据项？').then(function() {
        return delActivity(activityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>